<%@include file="_header.jspf" %>
<%@page import="com.spartancoder.accommodation.models.AuthToken" %>
<%@page import="com.spartancoder.accommodation.models.Administrator" %>
<%@page import="com.spartancoder.accommodation.beans.AdministratorBean" %>
<%
int adminId = Integer.parseInt(request.getParameter("id"));

AuthToken authToken = (AuthToken)request.getAttribute("authToken");

String yourPassword = request.getParameter("yourPassword");
String newPassword = request.getParameter("newPassword");
String newPasswordConfirm = request.getParameter("newPasswordConfirm");

String error = null;
if( yourPassword != null && newPassword != null && newPasswordConfirm != null ) {
    if(yourPassword.trim().isEmpty() ||
       newPassword.trim().isEmpty() ||
       newPasswordConfirm.trim().isEmpty() ) {
       error = "Niciun camp nu trebuie sa fie gol";
    } else {
        AdministratorBean ab = Administrator.fetch(authToken.getAdministrator().getUser(), yourPassword);
        if (ab == null) {
            error = "Parola personala invalida";
        } else {
            if (newPassword.equals(newPasswordConfirm) == false) {
                error = "Parola confirmata nu corespunde";
            } else {
                Administrator.updatePassword(adminId, newPassword);
                session.setAttribute("flash", "Parola a fost schimbata");
                response.sendRedirect("list-admin.jsp");
            }
        }
    }
}

%>

<h2>Schimbare parola</h2>

<form action="password-admin.jsp" method="post" class="span-10">
    <fieldset>
        <input type="hidden" name="id" value="<%=adminId%>" />

        <% if(error != null) { %>
            <br /><div class="error"><%=error%></div>
        <% } %>

        <label class="text large">Parola ta:</label><br />
        <input type="password" name="yourPassword" class="text large" /><br />

        <label class="text large">Noua parola:</label><br />
        <input type="password" name="newPassword" class="text large" /><br />

        <label class="text large">Confirmare Parola:</label><br />
        <input type="password" name="newPasswordConfirm" class="text large" /><br />

        <button type="submit" class="button positive">
            Schimba parola
        </button>

    </fieldset>
</form>

<%@include file="_footer.jspf" %>